package com.company.cloud.user.client;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.user.modules.api.response.TeamSimpleModel;
import com.company.cloud.user.modules.base.entity.TeamInfo;
import com.company.cloud.user.modules.base.service.TeamInfoService;
import com.company.cloud.user.modules.cache.models.UserShareCodeModel;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.user.modules.api.response.UserFull;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.cloud.user.modules.app.service.UserRegisterService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.company.cloud.user.modules.cache.service.UserShareCodeCacheService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;

@Log4j2
@RestController
@Service
public class UserRemoteServiceImpl implements UserRemoteService{

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserRegisterService userRegisterService;
	@Autowired
	private UserShareCodeCacheService userShareCodeCacheService;
	@Autowired
	private TeamInfoService teamInfoService;

	@Override
	public BaseClientResult<UserSimple> getUserById(Integer id) {
		try {
			UserInfo userInfo = userInfoService.getById(id);
			if (userInfo != null) {
				return BaseClientResult.success(UserConvertUtils.getUserSimple(userInfo));
			} else {
				return BaseClientResult.success();
			}
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}
	
	@Override
	public BaseClientResult<UserFull> getFullUserById(Integer id) {
		try {
			UserInfo userInfo = userInfoService.getById(id);
			if (userInfo != null) {
				return BaseClientResult.success(UserConvertUtils.getUserFull(userInfo));
			} else {
				return BaseClientResult.success();
			}
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}
	

	@Override
	public BaseClientResult<List<UserSimple>> getUserListByIds(String ids) {
		try {
			Set<Integer> userIds = StrSplitUtils.parse2set(ids, Integer.class);
			return this.getUserListByIds(userIds);
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<List<UserSimple>> getUserListByIds(Set<Integer> ids) {
		try {
			if (CollectionUtils.isEmpty(ids)) {
				return null;
			}
			List<UserInfo> userInfo = userInfoService.list(
					new QueryWrapper<UserInfo>().in("id", ids)
			);
			List<UserSimple> result = new ArrayList<UserSimple>();
			if (!CollectionUtils.isEmpty(userInfo)) {
				userInfo.forEach(item -> {
					result.add( UserConvertUtils.getUserSimple(item));
				});
			}
			return BaseClientResult.success(result);
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<Map<Integer, UserSimple>> getUserMapByIds(String ids) {
		try {
			Set<Integer> userIds = StrSplitUtils.parse2set(ids, Integer.class);
			return getUserMapByIds(userIds);
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<Map<Integer, UserSimple>> getUserMapByIds(Set<Integer> ids) {
		try {
			if (CollectionUtils.isEmpty(ids)) {
				return BaseClientResult.success();
			}
			BaseClientResult<List<UserSimple>> listResp = this.getUserListByIds(ids);
			if (BaseReturnEnum.SUCCESS.getCode().equals(listResp.getCode())) {
				Map<Integer, UserSimple> map =
						listResp.getData().stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
				return BaseClientResult.success(map);
			} else {
				return BaseClientResult.error(listResp.getCode(), listResp.getMsg());
			}

		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<UserSimple> getUserByShareCode(String shareCode) {
		try {
			UserShareCodeModel userShareCodeModel = userShareCodeCacheService.getUserByShareCode(shareCode);
			if (userShareCodeModel != null) {
				UserInfo userInfo = userInfoService.getById(userShareCodeModel.getUserId());
				return BaseClientResult.success(UserConvertUtils.getUserSimple(userInfo));
			}
			return BaseClientResult.success();

		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<Map<Integer, UserSimple>> getUserByShareCodes(Set<String> shareCodes) {
		try {
			if (CollectionUtils.isEmpty(shareCodes)) {
				return BaseClientResult.success();
			}
			Map<Integer, UserSimple> map = new HashMap<>();
			shareCodes.forEach(shareCode->{
				BaseClientResult<UserSimple> userSimpleResp = getUserByShareCode(shareCode);
				if (BaseReturnEnum.SUCCESS.getCode().equals(userSimpleResp.getCode())) {
					if (userSimpleResp.getData() != null) {
						map.put(userSimpleResp.getData().getId(), userSimpleResp.getData());
					}
				}
			});
			return BaseClientResult.success(map);
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<UserSimple> getUserByCustCode(String custCode) {
		try {
			 if(StrUtil.isEmpty(custCode)){
				 return BaseClientResult.error();
			 }
			 List<UserInfo> list = userInfoService.list(new QueryWrapper<UserInfo>().eq("cust_code",custCode));
			 if(CollUtil.isNotEmpty(list)){
				 UserSimple data=UserConvertUtils.getUserSimple(list.get(0));
				 return BaseClientResult.success(data);
			 }
			return BaseClientResult.success();
		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

	@Override
	public BaseClientResult<TeamSimpleModel> getTeamSimpleModel(Integer userId) {
		try {
			List<TeamInfo> list = teamInfoService.list(new QueryWrapper<TeamInfo>()
					.inSql("id", "select team_id from cust_team_user_info where user_id='"+userId+"'")
			);
			if (CollectionUtils.isEmpty(list)) {
				return null;
			} else {
				return BaseClientResult.success(UserConvertUtils.getTeamSimpleModel(list.get(0)));
			}

		} catch (Exception e) {
			return BaseClientResult.error();
		}
	}

}
